package com.vehicle.controller;

import com.vehicle.bean.DataGridView;
import com.vehicle.bean.PageVo;
import com.vehicle.bean.ResultDto;
import com.vehicle.pojo.ApplyUse;
import com.vehicle.pojo.Car;
import com.vehicle.pojo.Check;
import com.vehicle.pojo.User;
import com.vehicle.service.ApplyUseService;
import com.vehicle.service.CarExamineService;
import com.vehicle.service.CarService;
import com.vehicle.service.SendCarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("sendCar")
public class SendCarController {

    @Autowired
    private SendCarService sendCarService;

    @Autowired
    private ApplyUseService applyUseService;

    @Autowired
    private CarExamineService carExamineService;

    @Autowired
    private CarService carService;

    //寻找所有未派车的申请
    @RequestMapping("findAllApplyUse")
    @ResponseBody
    public DataGridView findAllApplyUse(Integer deptid, PageVo pageVo){
        return sendCarService.findAllApplyUseByStatus(deptid,pageVo);
    }
    //跳转派遣车辆页面
    @RequestMapping("sendCarCheck")
    public ModelAndView CarExamineCheck(Integer applyid){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/page/car/sendCarCheck.jsp");
        ApplyUse applyUse = applyUseService.selectApplyUseById(applyid);
        modelAndView.addObject("applyUse",applyUse);
        Check check = carExamineService.selectCheckByApplyid(applyid);
        modelAndView.addObject("check",check);
        List<Car> carList = carExamineService.selectCarByLazy();
        modelAndView.addObject("carList",carList);
        return modelAndView;
    }
    @RequestMapping("sendCarAdd")
    public ResultDto sendCarAdd(Integer carid, Integer applyid, HttpSession session){
        ResultDto resultDto = new ResultDto();
        try {
            Car car = carService.findCarByCarid(carid);
            Date sendCarTime = new Date();
            User user = (User) session.getAttribute("USER_SESSION");
            String sendCarUser = user.getRealname();
            applyUseService.updateCarPlateByApplyId(car.getCarplate(),applyid,sendCarTime,sendCarUser);
            applyUseService.updateApplyUseState(applyid,1);
            carService.updateCarable(carid,1);
            resultDto.setCode(1001);
            resultDto.setMsg("派车成功！");
        }catch (Exception e){
            e.printStackTrace();
            resultDto.setCode(-1001);
            resultDto.setMsg("派车失败！");
        }
        return resultDto;
    }

}
